<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePurchaseTables extends Migration
{
	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		$prefix = config('ibrand.app.database.prefix', 'ibrand_');
		if (!Schema::hasTable($prefix . 'store_purchase')) {  //采购、退货表
			Schema::create($prefix . 'store_purchase', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('shop_id')->comment('店铺id');
				$table->string('code', 100)->comment('业务编码');
				$table->unsignedInteger('clerk_id')->comment('采购人');
				$table->string('type', 30)->default('purchase')->comment('采购:purchase 退货:reject');
				$table->string('remark')->nullable()->comment('备注');
				$table->unsignedInteger('quantity')->comment('数量');
				$table->decimal('total')->comment('总价');
				$table->tinyInteger('status')->default(1)->comment('1:正常；2：草稿');
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'store_purchase_item')) {  //采购、退货明细表
			Schema::create($prefix . 'store_purchase_item', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('purchase_id');
				$table->string('item_type');
				$table->unsignedInteger('item_id');
				$table->unsignedInteger('quantity')->comment('数量');
				$table->decimal('price')->comment('单价');
				$table->decimal('discount')->default(100)->comment('折扣');
				$table->decimal('purchase_price')->comment('采购价');
				$table->decimal('total')->comment('总价');
				$table->text('meta')->nullable(); //detail_id:商品ID；color:颜色;size：尺码；
				$table->unsignedInteger('goods_id');

				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'store_take_stock')) {  //库存盘点表
			Schema::create($prefix . 'store_take_stock', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('shop_id')->comment('店铺id');
				$table->string('code', 100)->comment('业务编码');
				$table->unsignedInteger('clerk_id')->comment('采购人');
				$table->unsignedInteger('total')->default(0)->comment('盘点总数');
				$table->integer('difference_count')->default(0)->comment('盈亏数量');
				$table->decimal('difference_amount')->default(0)->comment('盈亏金额');
				$table->string('remark')->nullable()->comment('备注');
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'store_take_stock_item')) {  //库存盘点明细表
			Schema::create($prefix . 'store_take_stock_item', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('take_stock_id');
				$table->string('item_type');
				$table->unsignedInteger('item_id');
				$table->unsignedInteger('count')->default(0)->comment('盘点数量');
				$table->unsignedInteger('before_count')->default(0)->comment('盘点前数量');
				$table->integer('difference_count')->default(0)->comment('盈亏数量');
				$table->decimal('difference_amount')->default(0)->comment('盈亏金额');
				$table->text('meta')->nullable(); //detail_id:商品ID；color:颜色;size：尺码；
				$table->unsignedInteger('goods_id');

				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'store_stock_record')) {  //库存流水表
			Schema::create($prefix . 'store_stock_record', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('shop_id')->comment('店铺id');
				$table->unsignedInteger('item_id');
				$table->string('item_type');
				$table->string('type', 50)->comment('业务类型');
				$table->string('business_type')->comment('业务类型模型');
				$table->unsignedInteger('business_type_id')->comment('业务类型模型id');
				$table->integer('change_count')->comment('变化数量');
				$table->unsignedInteger('current_stock')->default(0)->comment('当前库存');
				$table->unsignedInteger('goods_id');
				$table->string('meta')->nullable(); //detail_id:商品ID；color:颜色;size：尺码；

				$table->timestamps();
				$table->softDeletes();
			});
		}
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		$prefix = config('ibrand.app.database.prefix', 'ibrand_');

		Schema::dropIfExists($prefix . 'store_purchase');
		Schema::dropIfExists($prefix . 'store_purchase_item');
		Schema::dropIfExists($prefix . 'store_take_stock');
		Schema::dropIfExists($prefix . 'store_take_stock_item');
		Schema::dropIfExists($prefix . 'store_stock_record');
	}
}
